package com.yeyks.commonReference.service.other.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.yeyks.account.dal.domain.AccountInfo;
import com.yeyks.common.constants.AccountType;
import com.yeyks.common.dto.hotel.HotelChinaProvinceDTO;
import com.yeyks.common.param.cnhotel.HotelCountParam;
import com.yeyks.common.result.screen.DataGreenHotelTotalResult;
import com.yeyks.common.result.screen.DataGreenVipHotelMonthResult;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.hotel.HotelInfoService;
import com.yeyks.commonReference.service.other.DataCenterService;
import com.yeyks.other.dal.domain.AddressDbCn;
import com.yeyks.other.service.AddressDbCnService;
import com.yeyks.search.service.MySearchService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author dagny
 * @date 2020/1/2 0002 16:13
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DataCenterServiceImpl implements DataCenterService {

    private final AddressDbCnService addressDbCnService;

    private final MySearchService mySearchService;

    private final AccountInfoService accountInfoService;

    private final HotelInfoService hotelInfoService;

    /**
     * 所有酒店数量和省份统计
     *
     * @return 返回结果
     */
    @Override
    public DataGreenHotelTotalResult statHotelTotal() {
        DataGreenHotelTotalResult result = new DataGreenHotelTotalResult();
        List<HotelChinaProvinceDTO> dtoList = Lists.newArrayList();
        List<AddressDbCn> list = addressDbCnService.list();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(addressDbCn -> {
                HotelChinaProvinceDTO dto = new HotelChinaProvinceDTO();
                dto.setName(addressDbCn.getName());
                dto.setId(addressDbCn.getDivCode());
                //计算数量（自营加酒店）
                dto.setValue(convertHotelCount(addressDbCn.getDivCode(), addressDbCn.getDivCodeCn()));
                dtoList.add(dto);
            });
            //计算总酒店数
            long total = dtoList.stream().mapToLong(HotelChinaProvinceDTO::getValue).sum();
            result.setHotelTotalCount(total);
            result.setList(dtoList);
        }

        return result;
    }

    /**
     * 优享VIP 酒店统计（按近六月份统计）
     *
     * @return 返回结果
     */
    @Override
    public List<DataGreenVipHotelMonthResult> statVipHotelMonth() {

        return hotelInfoService.statVipHotelSixMonth();
    }

    /**
     * 计算自营 + cn的结果
     *
     * @param divCode db 的省份code
     * @param cnCode  cn 的省份code
     * @return 返回 cn和自营的总和
     */
    private Long convertHotelCount(String divCode, String cnCode) {

        //查询自营的酒店
        long dbCount = accountInfoService.count(new QueryWrapper<AccountInfo>()
                .eq(AccountInfo.PROVINCE_CODE, divCode)
                .eq(AccountInfo.TYPE, AccountType.HotelManager));
        //查询cn的酒店
        HotelCountParam param = new HotelCountParam();
        param.setProvinceId(cnCode);
        Integer cnCount = mySearchService.searchHotelCount(param);
        return dbCount + cnCount;
    }
}
